package com.yiqi.lottery.feature.mine.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.OrderListBo;
import com.yiqi.lottery.feature.home.contract.PayContract;
import com.yiqi.lottery.feature.home.dependencies.DaggerPayComponent;
import com.yiqi.lottery.feature.home.dependencies.PayPresenterModule;
import com.yiqi.lottery.feature.home.view.PayActivity;

import javax.inject.Inject;

public class BillingActivity extends BaseActivity implements
        RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener, PayContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_billing_type)
    TextView mTvBillingType;
    @BindView(R.id.tv_billing_money)
    TextView mTvBillingMoney;
    @BindView(R.id.radioGroup)
    RadioGroup mRadioGroup;
    @BindView(R.id.edt_bzpv)
    CheckBox mEdtBzpv;
    @BindView(R.id.edt_billing_pv)
    EditText mEdtBillingPv;
    @BindView(R.id.cbx_gendan)
    CheckBox mCbxGendan;
    @BindView(R.id.cbx_tuiguang)
    CheckBox mCbxTuiguang;
    @BindView(R.id.btn_confrim)
    TextView mBtnConfrim;
    @BindView(R.id.edt_faxy)
    EditText mEdtFaxy;
    private boolean isOrder = false;

    ProgressDialog progressDialog;

    String mLottoData = "", mBetList = "";
    String mIssus = "";
    int mMultiple, mMut_num, mIs_charsing, mCharsing_num;

    private Integer mOrderId;
    private int mType = 0;
    private int mTotalAmount, mMyMoney;
    private boolean isClick = false;

    private int is_open = 1;
    private int is_follow = 1;
    private double safe_rate = 0.00;
    private String follow_intro;
    private double order_amount = 0.00;
    String share, percentage, subscribe, guarantee, declaration, desc, finish_time, min_reward, max_reward;


    @Inject
    PayContract.Presenter mPresenter;

    private OrderListBo mOrderListBo;
    private int is_spread = 2;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_billing;
    }

    @Override
    protected void initInjector() {
        DaggerPayComponent.builder().payPresenterModule(new PayPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "发单", true, true);
    }

    @Override
    protected void initData() {




        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("订单确认中，请稍等......");//3.设置显示内容
        progressDialog.setCancelable(false);//4.设置可否用back键关闭对话框
        mPresenter.doGetUserInfo();
        mLottoData = this.getIntent().getStringExtra("data");
        mBetList = this.getIntent().getStringExtra("bet_list");
        mIssus = this.getIntent().getStringExtra("issue");
        mType = this.getIntent().getIntExtra("order_type", 1);

        mMultiple = this.getIntent().getIntExtra("multiple", 1);
        mMut_num = this.getIntent().getIntExtra("mut_num", 1);
        mOrderId = this.getIntent().getIntExtra("order_id", 0);
        mIs_charsing = this.getIntent().getIntExtra("is_charsing", 2);
        mCharsing_num = this.getIntent().getIntExtra("charsing_num", 1);
        mTotalAmount = this.getIntent().getIntExtra("total_amount", 0);
        mMyMoney = this.getIntent().getIntExtra("my_money", 0);

        share = this.getIntent().getStringExtra("share");
        percentage = this.getIntent().getStringExtra("percentage");
        subscribe = this.getIntent().getStringExtra("subscribe");
        guarantee = this.getIntent().getStringExtra("guarantee");
        declaration = this.getIntent().getStringExtra("declaration");
        desc = this.getIntent().getStringExtra("desc");
        finish_time = this.getIntent().getStringExtra("finish_time");
        min_reward = this.getIntent().getStringExtra("min_reward");
        max_reward = this.getIntent().getStringExtra("max_reward");

        is_open = this.getIntent().getIntExtra("is_open", 1);
        safe_rate = this.getIntent().getDoubleExtra("safe_rate", 0.00);
        follow_intro = this.getIntent().getStringExtra("follow_intro");
        isOrder=this.getIntent().getBooleanExtra("is_order",false);

        mOrderListBo = (OrderListBo) this.getIntent().getSerializableExtra("info");
        if (isOrder){
            if (mOrderListBo != null) {
                mTvBillingType.setText("投注彩种:" + mOrderListBo.getBet_label());
                mTvBillingMoney.setText(Html.fromHtml("方案金额：<font color=\"#D83200\">" + mOrderListBo.getOrder_amount() + "</font> "));
            }
        }else {
            mTvBillingType.setText("投注彩种:" + "竟足");
            mTvBillingMoney.setText(Html.fromHtml("方案金额：<font color=\"#D83200\">" + mTotalAmount+ "</font> "));

        }
    }


    @OnClick(R.id.btn_confrim)
    public void onViewClicked() {
        if (mEdtBzpv.isChecked()) {
            if (TextUtils.isEmpty(mEdtBillingPv.getText().toString())) {
                showMsg("请输入赔率！");
                return;
            }

            if (Double.valueOf(mEdtBillingPv.getText().toString()) <= 0) {
                showMsg("请输入有效赔率！");
                return;
            }
            safe_rate = Double.valueOf(mEdtBillingPv.getText().toString());
        } else {
            mEdtBillingPv.setText(null);
            safe_rate = 0.00;
        }

        if (TextUtils.isEmpty(mEdtFaxy.getText().toString())) {
            showMsg("请输入方案宣言！");
            return;
        }

        if (isOrder){
            mPresenter.billingOrder(mOrderListBo.getOrder_id() + "", is_open, safe_rate, mEdtFaxy.getText().toString(), is_spread, is_follow);
        }
        else {
            Intent intent = new Intent(this, PayActivity.class);
            intent.putExtra("order_type", mType);
            intent.putExtra("data", mLottoData);
            intent.putExtra("multiple", mMultiple);
            intent.putExtra("mut_num", mMut_num);
            intent.putExtra("issue", mIssus);
            intent.putExtra("total_amount", 2 * mMultiple * mMut_num);
            intent.putExtra("is_charsing", 2);
            intent.putExtra("charsing_num", 1);
            intent.putExtra("min_reward", min_reward);
            intent.putExtra("max_reward", max_reward);
            intent.putExtra("is_follow", is_follow);
            intent.putExtra("is_open", is_open);
            intent.putExtra("safe_rate", safe_rate);
            intent.putExtra("follow_intro", mEdtFaxy.getText().toString());
            startActivity(intent);
        }


    }

    @Override
    protected void initEvent() {
        mRadioGroup.setOnCheckedChangeListener(this);
        mCbxGendan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_follow=isChecked?1:2;
            }
        });
        mCbxGendan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_spread = isChecked ? 1 : 2;
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_gk:
                is_open = 1;
                break;
            case R.id.rb_kshgk:
                is_open = 2;
                break;
            case R.id.rb_bm:
                is_open = 3;
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void showUserInfoSuccess(UserInfoBo userInfoBo) {

    }

    @Override
    public void showUserInfoError(String msg) {

    }

    @Override
    public void showBillingSuccess() {
        showMsg("发单成功");
        finish();
    }

    @Override
    public void showCreateOrderStart() {

    }

    @Override
    public void showCreateOrderSuccess(String luckyHistoryBo) {

    }

    @Override
    public void showFollowOrderSuccess(String msg) {

    }

    @Override
    public void showError(String ex) {
        showMsg(ex);
    }
}
